﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using yyylib;
using System.Diagnostics;

namespace TmEditer
{
    public partial class FromMain : Form
    {
        public FromMain()
        {
            InitializeComponent();
        }
        private GameObject gameobj;
        private void Form1_Load(object sender, EventArgs e)
        {
            if (ProgramUtil.ProcExist("MonsterTrain")) { 
                DialogResult result = MessageBox.Show("先退出游戏再运行修改器,是否退出?", "请先退出游戏", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes) {
                    ProgramUtil.CloseProc("MonsterTrain");
                }
            }
            gameobj = new GameObject();
            try {
                FileVersionInfo fv = FileVersionInfo.GetVersionInfo("TmEditer.dll");
                this.lable_info.Text = fv.FileMajorPart+""+fv.FileMinorPart +"-"+ fv.FileVersion;
            }
            catch (Exception ecp)
            {
                this.lable_info.Text = ecp.Message;
            }
        }


        private void FormActive(object sender, EventArgs e)
        {
            if (gameobj.load()) {
                DialogResult result = MessageBox.Show("您的游戏已经产生新的存档,如果不重新加载,将会使用当前存档,是否重新加载?", "发现新的存档", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes){
                    gameobj.load(true);
                }
            }
            this.RefreshInfo();
        }

        private void RefreshInfo()
        {
            textGold.Text = "" +gameobj.Gold;
            textHp.Text = "" + gameobj.Hp;
            textHpMax.Text = "" + gameobj.HpMax;
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            gameobj.Gold = int.Parse(textGold.Text);
            gameobj.Hp = int.Parse(textHp.Text);
            gameobj.HpMax = int.Parse(textHpMax.Text);
            if (gameobj.Save()) {
                MessageBox.Show("保存成功,请打开游戏");
            }
        }

        private void info_Click(object sender, EventArgs e)
        {

        }
    }
}
